<?php


namespace app\Utils;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use app\admin\model\System\SysSettingModel;
use app\api\model\User\UserVerifyCodeModel;

class AliDaYuSMS
{
    protected $appId;
    protected $secret;
    protected $sign;
    protected $template;
    protected $template_regiter;
    protected $template_update;

    static $acsClient = null;

    public function __construct()
    {
        //获取短信配置
//        $setting = SysSettingModel::getSysSettingWhere([['id','=',1]]);
//        if(!empty($setting)){
//            $aliyun = json_decode($setting['aliyun'],true);
//            $this->appId = $aliyun['app_id'];
//            $this->secret = $aliyun['secret'];
//            $this->sign = $aliyun['sign'];
//            $this->template = $aliyun['template'];
//        }else{
//            $this->appId = "";
//            $this->secret = "";
//            $this->sign = "";
//            $this->template = "";
//        }
        $this->appId = "LTAI4FzBkECsu37muCVZz4o8";
        $this->secret = "DHjLn62lVfQVI4YyM2W1V1QeCiYx9j";
        $this->sign = "师域";
        $this->template_regiter = "SMS_205361028"; //注册
        $this->template_update = "SMS_205361027"; //修改密码

    }

    public static function i()
    {
        $aliDaYuSMS = new AliDaYuSMS();
        return $aliDaYuSMS;
    }

    //发送短信
    public function sendSMS($mobile, $verify_code,$type=1)
    {
        try {
            AlibabaCloud::accessKeyClient($this->appId, $this->secret)
                ->regionId('cn-hangzhou')
                ->asDefaultClient();
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host('dysmsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => $mobile,
                        'SignName' => $this->sign,
                        'TemplateCode' => $type==1?$this->template_regiter:$this->template_update,
                        'TemplateParam' => json_encode([
                            'code' => $verify_code
                        ]),
                    ],
                ])
                ->request();
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }
        return ['code' => 0, 'msg' => "发送成功"];
    }

    //短信验证
    public function verifyCode($type, $mobile, $verify_code)
    {
        $code = UserVerifyCodeModel::getUserVerifyCodeWhere([['mobile', '=', $mobile], ['type', '=', $type], ['status', '=', 0]]);
        if (!empty($code)) {
            $time = strtotime($code['add_time']) + $code['time'];
            if ($time >= time()) {
                if ($code['code'] == $verify_code) {
                    $save = [
                        'status' => 1,
                        'up_time' => date("Y-m-d H:i:s", time())
                    ];
                    $flag = UserVerifyCodeModel::updateUserVerifyCodeWhere([['id', '=', $code['id']]], $save);
                    if ($flag) {
                        return ['code' => 0, 'msg' => "验证已通过"];
                    } else {
                        return ['code' => -1, 'msg' => "验证码错误"];
                    }
                } else {
                    return ['code' => -1, 'msg' => "验证码错误"];
                }
            } else {
                return ['code' => -1, 'msg' => "验证码已过期"];
            }
        } else {
            return ['code' => -1, 'msg' => "验证码无效"];
        }
    }


}